package cn.hxh.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2020/8/20 13:42
 */
@WebServlet("/session")
public class SessionServlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flag = req.getParameter("flag");
        HttpSession session = req.getSession();
        ServletContext servletContext = req.getServletContext();
        if (flag.equals("1")) {
            session.setAttribute("user", "小涵");
            servletContext.setAttribute("aa","小涵的servletContext");
        }
        if (flag.equals("2")) {
            session.setAttribute("user", "小潇");
            servletContext.setAttribute("aa","小潇的servletContext");
        }

        String user = (String) session.getAttribute("user");
        System.out.println(user+"登录了");
        System.out.println(servletContext.getAttribute("aa"));


    }
}
